import * as THREE from 'three'

import { flyArc } from '../../../Utils/arc'
// 添加飞线
const flyLineArcGroup = new THREE.Group()
export const addFlyLine = (data, threeInfo) => {
  const { radius, group } = threeInfo
  flyLineArcGroup.userData['flyLineArray'] = []
  group.add(flyLineArcGroup)

  data.forEach((cities) => {
    // 飞线终点坐标数据
    // 调用函数flyArc绘制球面上任意两点之间飞线圆弧轨迹
    cities.endArray.forEach((item: { lon: any; lat: any }) => {
      const arcLine = flyArc(
        radius,
        cities.startArray.lon,
        cities.startArray.lat,
        item.lon,
        item.lat,
        {
          color: 0x409eff, // 飞线的颜色
          flyLineColor: 0x00edff, // 飞行线的颜色
          speed: 0.005, // 拖尾飞线的速度
        }
      )
      // arcLine.name = 'flyLine'
      setTimeout(() => {
        flyLineArcGroup.add(arcLine) // 飞线插入flyArcGroup中
        flyLineArcGroup.userData['flyLineArray'].push(
          arcLine.userData['flyLine']
        )
      }, Math.random() * 5000)
    })
  })
}
// 飞线动画函数
export const flyLineAnimation = () => {
  flyLineArcGroup?.userData['flyLineArray']?.forEach((fly: any) => {
    fly.rotation.z += 0.004 // 调节飞线速度
    if (fly.rotation.z >= fly.flyEndAngle) fly.rotation.z = 0
  })
}

// 清除飞行线
export const clearFlyLines = () => {
  while (flyLineArcGroup.children.length > 0) {
    const flyLine = flyLineArcGroup.children[0]
    flyLineArcGroup.remove(flyLine)
  }

  flyLineArcGroup.userData['flyLineArray'] = []
}
